<?php
//session_start();
ob_start(); // clear buffer
/*
	EXPORT DATA DARI DATABASE KE EXCEL ATAU WORD
	Author : yudiehidayat at gmail dot com
*/
?>
<?php
include("config.php");
$Use_Title = 1; // kalo pake judul 1, gak 0
$title		= "MASTER KARYAWAN ";// nama judul
//$title .="<img src='../images/logo_ptsi.jpg'>";
$nama_file	= "MasterKaryawanPTSI"; // nama file
$now_date = date('m-d-Y H:i');
// get data dari sql
if(!empty($HTTP_GET_VARS['st'])){
	//$FILTER = "WHERE stat='".$HTTP_GET_VARS['st']."'";
		if(($HTTP_GET_VARS['st'] == "all" || empty($HTTP_GET_VARS['st'])) && ($HTTP_GET_VARS['divisi'] == "all" || empty($HTTP_GET_VARS['divisi']))){
			$sql = "WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY NIK) AS No, * FROM Karyawan WHERE Nama LIKE '%$keyword%' OR NIK LIKE '%$keyword%' OR Alamat LIKE '%$keyword%') SELECT * FROM limit WHERE No BETWEEN ".$offset." AND ".($offset+$limit)."";
		}
		else{
			$sql = "WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY NIK) AS No, * FROM VLatestKarir WHERE (Nama LIKE '%$keyword%' OR NIK LIKE '%$keyword%' OR Alamat LIKE '%$keyword%') ";
			
			if(!empty($HTTP_GET_VARS['st'])){
				if($HTTP_GET_VARS['st'] == "Berhenti"){
					$sql .= " AND Jenis IN ('H','U') ";			
				}
				elseif($HTTP_GET_VARS['st'] == "Pelamar"){
					$sql .= " AND id_kar IS NULL";
				}
				elseif($HTTP_GET_VARS['st'] == "all"){
				}
				else{
					$sql .= " AND (stat_kar1 = '".$HTTP_GET_VARS['st']."' OR stat_kar2='".$HTTP_GET_VARS['st']."') ";
					//$sql .= " ) SELECT * FROM limit WHERE No BETWEEN ".$offset." AND ".($offset+$limit)."";
				}
			}
			
			if(!empty($HTTP_GET_VARS['divisi'])){
				if($HTTP_GET_VARS['divisi'] == "all"){
					//$sql .= ") SELECT * FROM limit WHERE No BETWEEN ".$offset." AND ".($offset+$limit)."";
				}
				else{
					$sql .= " AND ( substring(kode_so1,1,7)='".substr($HTTP_GET_VARS['divisi'],0,7)."' OR substring(kode_so2,1,7)='".substr($HTTP_GET_VARS['divisi'],0,7)."' )";
				}
			}
			
			$sql .= " ) SELECT * FROM limit ORDER BY Nama ASC";
	}
}
//else{
	//$FILTER = "";
//}
//echo $sql."<br><br>";
$result = mssql_query($sql,$conn);
if (isset($w) && ($w==1))
{
	$file_type = "msword";
	$file_ending = "doc";
}else {
	$file_type = "vnd.ms-excel";
	$file_ending = "xls";
}
// deklarasi output file
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=$nama_file.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");

// memulai format excel/word

if (isset($w) && ($w==1))
{
	/* UNTUK FORMAT WORD DOC*/
	if ($Use_Title == 1)
	{
		echo("$title\n\n");
	}
	// separator
	$sep = "\n"; 

	while($row = mssql_fetch_row($result))
	{
		$schema_insert = "";
		for($j=0; $j<mssql_num_fields($result);$j++)
		{
		// cari field yang sesuai dengan sql
		$field_name = mssql_field_seek($result,$j);
		// menampilkan nama field
		$schema_insert .= "$field_name:\t";
			if(!isset($row[$j])) {
				$schema_insert .= "NULL".$sep;
				}
			elseif ($row[$j] != "") {
				$schema_insert .= "$row[$j]".$sep;
				}
			else {
				$schema_insert .= "".$sep;
				}
		}
		$schema_insert = str_replace($sep."$", "", $schema_insert);
		$schema_insert .= "\t";
		print(trim($schema_insert));
		print "\n----------------------------------------------------\n";
	}
}else{
	/*FORMAT HANYA UNTUK EXCEL*/
	// buat title paging atau sheet
	if ($Use_Title == 1)
	{
		echo("$title\n");
	}
	$sep = "\t"; //tabbed character

	//cetak kolom dari database
	for ($i = 0; $i < mssql_num_fields($result); $i++)
	{
		//echo mssql_field_seek($result,1) . "\t";
		mssql_field_seek($result,$i);
		$finfo=mssql_fetch_field($result);
		echo $finfo->name;
		echo"\t";
	}
	print("\n");
	// end cetak kolom

	// looping data
	while($row = mssql_fetch_row($result))
	{
		$schema_insert = "";
		for($j=0; $j<mssql_num_fields($result);$j++)
		{
			if(!isset($row[$j]))
				$schema_insert .= "NULL".$sep;
			elseif ($row[$j] != "")
				$schema_insert .= "$row[$j]".$sep;
			else
				$schema_insert .= "".$sep;
		}
		$schema_insert = str_replace($sep."$", "", $schema_insert);
		$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
		$schema_insert .= "\t";
		print(trim($schema_insert));
		print "\n";
	}
}

?>
